<template>
  <div>
    <!-- 头部导航 -->
    <van-nav-bar title="标题" class="page-nav" />
    <van-icon @click="$router.go(-1)" class="my-icon" slot="left" name="cross"></van-icon>
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref="logForm">
      <van-field v-model="mobile" name="mobile" placeholder="请输入手机号"> <i slot="left-icon" class="toutiao toutiao-shouji"
          :rules="refRlue.mobile" type="number" maxlength="11"></i></van-field>
      <van-field v-model="code" name="code" placeholder="请输入验证码" :rules="refRlue.code" type="number" maxlength="6"><i
          slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <!-- 验证码框 -->
        <template #button>
          <van-count-down v-if="isCountDownShow" slot="button" :time="1000 * 5" format="ss s"
            @finish="isCountDownShow = false" />
          <van-button @click="editFrom" class="send-sms-btn" size="small" type="primary" native-type="button">发送验证码
          </van-button>
        </template>
      </van-field>
      <!-- 登录按钮 -->
      <div class="login-submit">
        <van-button round block type="info" native-type="submit">登录</van-button>

      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendCode } from '../../api/users'
export default {
  data () {
    return {
      isCountDownShow: false,
      mobile: '',
      code: '',
      refRlue: {
        mobile: [{
          required: true,
          message: '手机号不能为空'
        }, {
          pattern: /^[1][3,4,5,7,8,9][0-9]{9}$/,
          message: '手机号格式错误'
        }],
        code: [{ required: true, message: '不能为空' }, {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }]
      }

    }
  },
  created () { },
  methods: {
    // 1获取表单数据
    async onSubmit () {
      // 2进行验证
      // 3发送请求
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      try {
        const res = await login({
          mobile: this.mobile,
          code: this.code
        })
        console.log(res)
        // 储存到vuex
        this.$store.commit('userData', res.data.data)
        this.$toast.success('登陆成功')
        this.$router.push('/my')
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('登陆账号有误')
        }
        console.log(err)
        console.log('系统异常登陆失败')
      }
    },
    // 验证码校验异步通过async修饰
    async editFrom () {
      try {
        await this.$refs.logForm.validate('mobile')
        console.log('校验成功')
      } catch (err) {
        return console.log('校验失败')
      }
      // 加载
      this.$toast.loading({
        message: '发送中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      // 3发送短信验证码
      try {
        await sendCode(this.mobile)
        // 2 验证通过发送倒计时
        this.isCountDownShow = true
        this.$toast.success('发送成功')
      } catch (err) {
        if (err.response && err.response.status === 429) {
          return this.$toast.fail('发送频繁')
        }
        this.$toast.fail('发送异常')
      }
    }

  }
}
</script>

<style lang="less" scoped>
.toutiao {
  font-size: 37px;
}
.send-sms-btn {
  width: 152px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  border: none;
}
// 提交盒子
.login-submit {
  margin: 53px 28px;
  // 登录按钮
  .van-button {
    width: 694px;
    height: 88px;
    background-color: #6db4fb;
    border: none;
    border-radius: 10px;
  }
}
</style>
